package com.wk.springbootlearn.common.aop;

import com.google.gson.Gson;
import com.wk.springbootlearn.common.model.CommonLog;
import com.wk.springbootlearn.mapper.impl.LogMapperImpl;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.SuccessCallback;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

//controller层请求日志记录
@Aspect
@Component
@Slf4j
public class ControllerLogAspect {

    @Autowired
    private LogMapperImpl logMapper;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Pointcut("execution (* com.wk.springbootlearn.controller.*.*(..))")
    public void actPath(){}

    @Before("actPath()")
    public void before(JoinPoint joinPoint) {
        // 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        // 请求方法
        String method = request.getMethod();
        String requestURI = request.getRequestURI();
        String ip = request.getRemoteAddr();
        String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        CommonLog logInfo = new CommonLog().setHttpMethod(method).setUrl(requestURI).setIp(ip).setErrorMsg(classMethod);
        log.info(logInfo.toString());
        // 向mongodb中记录日志
//        logMapper.addOneLog(logInfo);
        // 向kafka中写入日志
//        kafkaTemplate.send("hello", new Gson().toJson(logInfo))
//                .addCallback(new SuccessCallback<SendResult<String, String>>() {
//                    @Override
//                    public void onSuccess(SendResult<String, String> stringStringSendResult) {
//                        if (stringStringSendResult.getRecordMetadata() != null) {
//                            System.out.println("消息发送成功 offset: " + stringStringSendResult.getRecordMetadata().offset());
//                            return;
//                        }
//                        System.out.println("消息发送成功");
//                    }
//                }, new FailureCallback() {
//                    @Override
//                    public void onFailure(Throwable throwable) {
//                        System.out.println("消息发送失败：" + throwable.getMessage());
//                    }
//                });
    }

    @After("actPath()")
    public void after() {}
}
